package com.examination_system.controller;

import com.examination_system.model.entity.User;
import com.examination_system.model.query.UserQuery;
import com.examination_system.service.UserService;
import com.examination_system.service.impl.UserServiceImpl;
import com.examination_system.utils.LogUtil;
import com.examination_system.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

@WebServlet("/list")
public class UserListServlet extends HttpServlet {
    private static final Logger LOGGER = LogUtil.getLogger();
    private UserService service = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = (String) req.getParameter("name");
        String userTypeStr = (String) req.getParameter("userType");
        String userNo = (String) req.getParameter("userNo");
        UserQuery query = new UserQuery();
        if (StringUtils.isNotBlank(userTypeStr)){
            Integer userType = Integer.parseInt(userTypeStr);
            query.setUserType(userType);
        }

        if (StringUtils.isNotBlank(name)){
            query.setName(name);
        }

        if (StringUtils.isNotBlank(userNo)){
            query.setUserNo(userNo);
        }

        List<User> list = service.queryList(query);
        for (User user : list) {
            System.out.println(user.toString());
        }
        req.setAttribute("list", list);
        req.getRequestDispatcher("/crud.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}